package com.xyzq.webapp.action.common;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzq.model.common.DataDictionary;
import com.xyzq.service.common.DataDictionaryManager;
import com.xyzq.webapp.action.BaseAction;

public class DataDictionaryAction extends BaseAction implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5147752887976796824L;
	private DataDictionaryManager dataDictionaryManager;
	private List<DataDictionary> modules;
	private List<DataDictionary> dataDictionaries;
	private DataDictionary dataDictionary;
	private Long id;

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		this.setModules(this.dataDictionaryManager.findByModule("module"));
	}

	public String list() {
		this.setDataDictionaries(this.getDataDictionaryManager().getAll());
		this.logUserAction("dataDicionary",
				"read list");
		return SUCCESS;
	}

	public String edit() {
		if (this.getId() != null) {
			this.setDataDictionary(this.getDataDictionaryManager().get(id));
			this.logUserAction("dataDicionary",
					"ready edit" + this.getDataDictionary());
		} else {
			this.setDataDictionary(new DataDictionary());
			this.logUserAction("dataDicionary",
					"ready input" + this.getDataDictionary());
		}
		return SUCCESS;
	}

	public String save() {
		if (cancel != null) {
			return CANCEL;
		}
		if (delete != null) {
			return delete();
		}
		boolean isNew = (this.getDataDictionary().getId() == null);
		this.getDataDictionary().setModifyTime(new Date());
		this.getDataDictionaryManager().save(getDataDictionary());
		String key = (isNew) ? "dataDictionary.added"
				: "dataDictionary.updated";
		saveMessage(getText(key));
		if (!isNew) {
			this.logUserAction("dataDicionary",
					"edit:" + this.getDataDictionary());
			return SUCCESS;
		} else {
			this.logUserAction("dataDicionary",
					"save:" + this.getDataDictionary());
			return INPUT;
		}

	}

	public String delete() {
		this.getDataDictionaryManager()
				.remove(this.getDataDictionary().getId());
		saveMessage(getText("dataDictionary.deleted"));
		this.logUserAction("dataDicionary",
				"delete:" + this.getDataDictionary());
		return SUCCESS;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setDataDictionary(DataDictionary dataDictionary) {
		this.dataDictionary = dataDictionary;
	}

	public DataDictionary getDataDictionary() {
		return dataDictionary;
	}

	public void setDataDictionaries(List<DataDictionary> dataDictionaries) {
		this.dataDictionaries = dataDictionaries;
	}

	public List<DataDictionary> getDataDictionaries() {
		return dataDictionaries;
	}

	public void setDataDictionaryManager(
			DataDictionaryManager dataDictionaryManager) {
		this.dataDictionaryManager = dataDictionaryManager;
	}

	public DataDictionaryManager getDataDictionaryManager() {
		return dataDictionaryManager;
	}

	public void setModules(List<DataDictionary> modules) {
		this.modules = modules;
	}

	public List<DataDictionary> getModules() {
		return modules;
	}

}
